package com.lyq.controller;

import com.lyq.mapper.HospitalMapper;
import com.lyq.pojo.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyq
 * @since 2020-05-08
 */
@Controller
@RequestMapping("/hospital")
public class HospitalContentController {
    @Autowired
    private HospitalMapper hospitalMapper;

    @GetMapping("/hospitalList")
    public String hospitalList(Model model){
        List<Hospital> hospitals = hospitalMapper.selectList(null);
         model.addAttribute("hospital",hospitals);
         return "hospital/hospitalList";

    }

    /**
     * to addHospital.html
     * @return
     */
    @GetMapping("/toAddHospital")
    public String toAddhospital(){
        return "hospital/addHospital";
    }
    @PostMapping("/addHospital")
    public String addHospital(Hospital hospital){
        int insert = hospitalMapper.insert(hospital);
        return "redirect:/hospital/hospitalList";
    }
    @GetMapping("/toUpdateHospital/{id}")
    public String toUpdateHospital(@PathVariable("id")Integer id,Model model){

        Hospital hospital = hospitalMapper.selectById(id);
        model.addAttribute("hospital",hospital);
        return "hospital/updateHospital.html";
    }
    @PostMapping("/updateHospital")
    public String updateHospital(Hospital hospital){
        int update = hospitalMapper.updateById(hospital);
        System.out.println(update);
        return "redirect:/hospital/hospitalList";
    }
    @GetMapping("/deleteHospital/{id}")
    public String deleteHospital(@PathVariable("id")Integer id){
        int i = hospitalMapper.deleteById(id);
        return "redirect:/hospital/hospitalList";
    }

}

